/**
 * 
 */
package com.hz.winnie.demands.web.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.hz.winnie.demands.common.DemandsConstants;
import com.hz.winnie.demands.pojo.Demand;
import com.hz.winnie.demands.pojo.Product;
import com.hz.winnie.demands.pojo.User;
import com.hz.winnie.demands.service.DemandsService;
import com.hz.winnie.demands.service.ProductService;

/**
 * @description 首页controller
 * @author winnie
 * @date 2011-4-6
 * @version 1.0
 */
@Controller
@RequestMapping("/")
public class IndexController {

    private static int limit = 10;

    @Autowired
    private DemandsService demandsService;

    @Autowired
    private ProductService productService;

    @Autowired
    private HttpSession session;

    @RequestMapping("/index")
    public String index(@RequestParam(defaultValue = "1") String currentPage,
	    Model model) {

	User demUser = (User) session
		.getAttribute(DemandsConstants.SESSION_KEY_USER);
	int userId = demUser.getId();

	// 我的需求个数
	Demand demand = new Demand();
	demand.setUserId(userId);
	int myDemCount = demandsService.countDemandsByCondition(demand);

	// 我的未完成个数
	String[] statuslist = { DemandsConstants.DEM_STATUS_UN_FOLLOW,
		DemandsConstants.DEM_STATUS_FOLLOWING };
	demand.setStatuslist(statuslist);
	int myUnDoDemCount = demandsService.countDemandsByCondition(demand);

	// 转给我的新需求个数
	demand = new Demand();
	demand.setIsNew("0");
	demand.setUserId(userId);
	int toMeNewDemCount = demandsService.countDemandsByCondition(demand);

	// 快过期需求个数
	demand = new Demand();
	demand.setStatuslist(statuslist);
	demand.setUserId(userId);
	demand.setStartTime(new Date());
	demand.setEndTime(DateUtils.addDays(new Date(), -7));
	int myCrashDemCount = demandsService.countDemandsByCondition(demand);

	// 已过期需求个数
	demand = new Demand();
	demand.setStatuslist(statuslist);
	demand.setUserId(userId);
	demand.setEndTime(new Date());
	int overdueDemCount = demandsService.countDemandsByCondition(demand);

	// 产品列表
	Product product = new Product();
	product.setLimit(limit);
	if (StringUtils.isBlank(currentPage)) {
	    currentPage = "1";
	}
	int start = (Integer.valueOf(currentPage) - 1) * limit;
	product.setStart(start);
	List<Product> productList = productService.listAllProduct(product);
	int countProducts = productService.countAllProduct();

	model.addAttribute("demUser", demUser);
	model.addAttribute("myDemCount", myDemCount);
	model.addAttribute("myUnDoDemCount", myUnDoDemCount);
	model.addAttribute("toMeNewDemCount", toMeNewDemCount);
	model.addAttribute("myCrashDemCount", myCrashDemCount);
	model.addAttribute("overdueDemCount", overdueDemCount);
	model.addAttribute("productList", productList);
	model.addAttribute("countProducts", countProducts);
	model.addAttribute("totalPages",
		countProducts % limit == 0 ? countProducts / limit
			: countProducts / limit + 1);
	model.addAttribute("currentPage", currentPage);
	return "index";
    }
}
